define([
    "angular",
    "framework/utils/JSONPrettyFormat",
    'framework/dataGrid/DataGridModule',
    'framework/pagination/GillionPaginationModule',
    'framework/msg/GillionMsgModule'

], function (angular, JSONPrettyFormat) {
    var BusinessModelModule = angular.module('BusinessModelModule', ['DataGridModule',
        'GillionPaginationModule',
        'GillionMsgModule'
    ]);

    BusinessModelModule.controller('BusinessModelController', function ($scope, Resource, $dataSourceManager, GillionMsg) {
        var businessModelResource = Resource("/grule-manager/businessModel/:id", {
            id: "@id"
        });

        $scope.showPageView = function () {
            $scope.showPage = true;
            $scope.showAdd = false;
            $scope.showEdit = false;
        };

        $scope.showPageView();

        $scope.searchBusinessModel = function () {
            $scope.search = $scope.search || {};
            var params = angular.extend($scope.search);
            $dataSourceManager.dataSources.businessModelSource.doRequestData(1, params);
        };

        $scope.onRemoveRecord = function (event, row) {
            GillionMsg.confirm('提示信息', '是否确定删除？', function(r){
                if (r) {
                    businessModelResource.remove({
                        id: row.id
                    }, function (response) {
                        if (response.success) {
                            GillionMsg.alert("提示消息", "删除成功！");
                            $scope.searchBusinessModel();
                        } else {
                            GillionMsg.alert("提示消息", response.errorMessages);
                        }
                    });
                }
            });
            return false;
        };

        $scope.showAddBusinessModel = function () {
            $scope.showAdd = true;
            $scope.showEdit = false;
            $scope.showPage = false;

            var templateBusinessModel = {
                "name": "【必填】",
                "remark": ""
            };
            $scope.addBusinessModel = JSONPrettyFormat(templateBusinessModel);
        };

        $scope.showEditBusinessModel = function () {
            $scope.showAdd = false;
            $scope.showEdit = true;
            $scope.showPage = false;
        };

        $scope.createBusinessModel = function () {
            var addBusinessModel = JSON.parse($scope.addBusinessModel);
            businessModelResource.create(addBusinessModel, function (response) {
                if (response.success) {
                    GillionMsg.alert("提示消息", "保存成功！");
                    $scope.search = $scope.search || {};
                    var params = angular.extend($scope.search);
                    $dataSourceManager.dataSources.businessModelSource.doRequestData(1, params);
                    $scope.showPageView();
                }else if (response.success !== false) {
                    GillionMsg.alert("提示消息", "保存失败，请核对保存信息！");
                    return;
                }
            });
        };

        $scope.businessModelRendered = function (grid) {
            grid.actions.edit = function (row) {
                businessModelResource.get({
                    id: row.id
                }, function (result) {
                    delete result.$promise;
                    delete result.$resolved;
                    $scope.currId = result.id;
                    delete result.id;
                    $scope.editBusinessModel = JSONPrettyFormat(result);
                    $scope.showEditBusinessModel();
                });
            };
        };

        $scope.updateBusinessModel = function () {
            var editBusinessModel = JSON.parse($scope.editBusinessModel);
            editBusinessModel.id = $scope.currId;
            businessModelResource.update(editBusinessModel, function (response) {
                if (response.success) {
                    GillionMsg.alert("提示消息", "更新成功！");
                    $scope.search = $scope.search || {};
                    var params = angular.extend($scope.search);
                    $dataSourceManager.dataSources.businessModelSource.doRequestData(1, params);
                    $scope.showPageView();
                } else if (response.success !== false) {
                    GillionMsg.alert("提示消息", "更新失败，请核对更新信息！");
                }
            });
        }
    });
    return BusinessModelModule;
});